// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: BG Animation Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//	
private alias 1 : TYPE_BGANIMATION

private alias object.value0 : object.scroll
private alias object.value1 : object.depth


private alias 0 : FACE_FLAG_TEXTURED_3D

//CD CODE LOL

event ObjectDraw	//draw order is bottom to top

	// The clouds move at a speed of 8 per second
	// No good units to good here so let's just leave it at that, "8"...


	//X MOVING
	temp3 = Object[2].Value2
	Object.Scroll += temp3
	Object.Scroll /= 2
	Object.Scroll -= 0x200
	
	// Flip it around, to make the Clouds move towards the screen and not away
	Temp0 = Object.Scroll
	FlipSign(Temp0)
	



	Draw3DScene()

	MatrixTranslateXYZ(MAT_WORLD, Temp0, -2000, -1000)

	object.drawOrder = 5

end event


event ObjectStartup
	LoadSpriteSheet("FNAF/TestWall.gif")
	SpriteFrame(0, 0, 241, 202, 1, 1)

	foreach (TypeName[Test Object], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
	next

	scene3D.vertexCount = 0
	scene3D.faceCount = 0
	
	// Note - the numbers getting set here are 0, but they're gonna be overwritten with 0 again a few lines later too
	ArrayPos0 = scene3D.vertexCount
	ArrayPos1 = scene3D.faceCount
	
	// Reset vertexes to their starting position
	MatrixTranslateXYZ(MAT_WORLD, 0, 0, 0)
	MatrixRotateXYZ(MAT_WORLD, 0, 0, 0)
	MatrixRotateXYZ(MAT_VIEW, -130, 0, 0)
	
	// Lots of values used here...
	// - ArrayPos0 is the index of the current Vertex
	// - ArrarPos1 is the index of the current Face
	// - temp0 is a counter for Cloud rows
	// - temp1 is the X Position of the Cloud
	// - temp2 is the Z Position of the Cloud
	// - temp3 is a counter for Cloud columns
	// - temp4 is the X corner of the cloud sprite for the vertex to use
	// - temp5 is the Y corner of the cloud sprite for the vertex to use
	
	ArrayPos0 = 0
	ArrayPos1 = 0
	temp2 = 0
	temp3 = 0
	temp4 = 0
	temp5 = 0
	
	// Make 10 Cloud columns
	while temp3 < 4
		
		temp0 = 0
		temp1 = -0x2000
		temp4 = 0
		
		// Make 16 Cloud rows	//left to right
		while temp0 < 8
			
			// Setup 4 vertexes, for a face
			// Each face is to be hold a 64x64 texture
			
			// Top left corner
			VertexBuffer[ArrayPos0].x = temp1
			VertexBuffer[ArrayPos0].y = 0
			VertexBuffer[ArrayPos0].z = temp2
			VertexBuffer[ArrayPos0].u = temp4
			VertexBuffer[ArrayPos0].v = temp5
			ArrayPos0++
			
			// Bottom left corner
			temp2 += 512
			VertexBuffer[ArrayPos0].x = temp1
			VertexBuffer[ArrayPos0].y = 0
			VertexBuffer[ArrayPos0].z = temp2
			VertexBuffer[ArrayPos0].u = temp4
			VertexBuffer[ArrayPos0].v = temp5
			VertexBuffer[ArrayPos0].v += 63
			ArrayPos0++
			
			// Top right corner
			temp2 -= 512
			temp1 += 1024
			VertexBuffer[ArrayPos0].x = temp1
			VertexBuffer[ArrayPos0].y = 0
			VertexBuffer[ArrayPos0].z = temp2
			VertexBuffer[ArrayPos0].u = temp4
			VertexBuffer[ArrayPos0].u += 63
			VertexBuffer[ArrayPos0].v = temp5
			ArrayPos0++
			
			// Bottom right corner
			temp2 += 512
			VertexBuffer[ArrayPos0].x = temp1
			VertexBuffer[ArrayPos0].y = 0
			VertexBuffer[ArrayPos0].z = temp2
			VertexBuffer[ArrayPos0].u = temp4
			VertexBuffer[ArrayPos0].u += 63
			VertexBuffer[ArrayPos0].v = temp5
			VertexBuffer[ArrayPos0].v += 63
			
			temp4 += 64
			temp4 &= 255
			temp2 -= 512
			
			// Move back to the index of the Top Left corner vertex
			ArrayPos0 -= 3
			
			// Now, setup the Face's corners
			
			// Assign the Top Left Vertex - [ArrayPos0 + 0]
			FaceBuffer[ArrayPos1].a = ArrayPos0
			
			// Assign the Top Right Vertex - [ArrayPos0 + 2]
			FaceBuffer[ArrayPos1].b = ArrayPos0
			FaceBuffer[ArrayPos1].b += 2
			
			// Assign the Bottom Left Vertex - [ArrayPos0 + 1]
			FaceBuffer[ArrayPos1].c = ArrayPos0
			FaceBuffer[ArrayPos1].c += 1
			
			// Assign the Bottom Right Vertex - [ArrayPos0 + 3]
			FaceBuffer[ArrayPos1].d = ArrayPos0
			FaceBuffer[ArrayPos1].d += 3
			
			// And then set up the Face's Flag to be a FACE_TEXTURED_3D flag, since the Cloud sprites are to be projected onto the face and all
			FaceBuffer[ArrayPos1].Flag = 0
			
			// Now, get ready for the next face
			
			// Bump Vertex index by 4
			ArrayPos0 += 4
			
			// Face Vertex only needs to be bumped by 1
			ArrayPos1++
			
			temp0++
			
			// And then we want it all to actually render too, so bump up the 3DScene rendering values as well
			scene3D.vertexCount += 4
			scene3D.faceCount++
			
		loop
		
		// And now, next column
		temp5 += 64
		temp5 &= 255
		temp2 += 512
		temp3++
		
	loop
	

end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
